Completed
Push — master ( d3c232...d777d8 )
by Johan
01:13
created

pBarcodeCall   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
// Import all the barcodes
2
import barcodes from './barcodes/';
3
4
// Help functions
5
import merge from './help/merge.js';
6
import linearizeEncodings from './help/linearizeEncodings.js';
7
import fixOptions from './help/fixOptions.js';
8
import getRenderProperties from './help/getRenderProperties.js';
9
10
// Exceptions
11
import ErrorHandler from './exceptions/ErrorHandler.js';
12
import {InvalidInputException} from './exceptions/exceptions.js';
13
14
// Default values
15
import defaults from './defaults/defaults.js';
16
17
// The protype of the object returned from the JsBarcode() call
18
let API = function(){};
19
20
// The first call of the library API
21
// Will return an object with all barcodes calls and the data that is used
22
// by the renderers
23
let JsBarcode = function(element, text, options){
24
	var api = new API();
25
26
	if(typeof element === "undefined"){
27
		throw Error("No element to render on was provided.");
28
	}
29
30
	// Variables that will be pased through the API calls
31
	api._renderProperties = getRenderProperties(element);
32
	api._encodings = [];
33
	api._options = defaults;
34
	api._errorHandler = new ErrorHandler(api);
35
36
	// If text is set, use the simple syntax (render the barcode directly)
37
	if(typeof text !== "undefined"){
38
		options = options || {};
39
40
		if(!options.format){
41
			options.format = autoSelectBarcode();
42
		}
43
44
		api.options(options)[options.format](text, options).render();
45
	}
46
47
	return api;
48
};
49
50
// To make tests work TODO: remove
51
JsBarcode.getModule = function(name){
52
	return barcodes[name];
53
};
54
55
// Register all barcodes
56
for(var name in barcodes){
57
	if(barcodes.hasOwnProperty(name)){ // Security check if the propery is a prototype property
58
		registerBarcode(barcodes, name);
59
	}
60
}
61
function registerBarcode(barcodes, name){
62
	API.prototype[name] =
63
	API.prototype[name.toUpperCase()] =
64
	API.prototype[name.toLowerCase()] =
65
		function(text, options){
66
			var api = this;
67
			return api._errorHandler.wrapBarcodeCall(function(){
68
				var newOptions = merge(api._options, options);
69
				var Encoder = barcodes[name];
70
				var encoded = encode(text, Encoder, newOptions);
71
				api._encodings.push(encoded);
72
73
				return api;
74
			});
75
		};
76
}
77
78
// encode() handles the Encoder call and builds the binary string to be rendered
79
function encode(text, Encoder, options){
80
	// Ensure that text is a string
81
	text = "" + text;
82
83
	var encoder = new Encoder(text, options);
84
85
	// If the input is not valid for the encoder, throw error.
86
	// If the valid callback option is set, call it instead of throwing error
87
	if(!encoder.valid()){
88
		throw new InvalidInputException(encoder.constructor.name, text);
89
	}
90
91
	// Make a request for the binary data (and other infromation) that should be rendered
92
	var encoded = encoder.encode();
93
94
	// Encodings can be nestled like [[1-1, 1-2], 2, [3-1, 3-2]
95
	// Convert to [1-1, 1-2, 2, 3-1, 3-2]
96
	encoded = linearizeEncodings(encoded);
97
98
	// Merge
99
	for(let i = 0; i < encoded.length; i++){
100
		encoded[i].options = merge(options, encoded[i].options);
101
	}
102
103
	return encoded;
104
}
105
106
function autoSelectBarcode(){
107
	// If CODE128 exists. Use it
108
	if(barcodes["CODE128"]){
109
		return "CODE128";
110
	}
111
112
	// Else, take the first (probably only) barcode
113
	return Object.keys(barcodes)[0];
114
}
115
116
// Sets global encoder options
117
// Added to the api by the JsBarcode function
118
API.prototype.options = function(options){
119
	this._options = merge(this._options, options);
120
	return this;
121
};
122
123
// Will create a blank space (usually in between barcodes)
124
API.prototype.blank = function(size){
125
	var zeroes = "0".repeat(size);
126
	this._encodings.push({data: zeroes});
127
	return this;
128
};
129
130
// Initialize JsBarcode on all HTML elements defined.
131
API.prototype.init = function(){
132
	// Make sure renderProperies is an array
133
	if(!Array.isArray(this._renderProperties)){
134
		this._renderProperties = [this._renderProperties];
135
	}
136
137
	var renderProperty;
138
	for(let i in this._renderProperties){
139
		renderProperty = this._renderProperties[i];
140
		var options = merge(this._options, renderProperty.options);
141
142
		if(options.format == "auto"){
143
			options.format = autoSelectBarcode();
144
		}
145
146
		var text = options.value;
147
148
		var Encoder = barcodes[options.format.toUpperCase()];
149
150
		var encoded = encode(text, Encoder, options);
151
152
		render(renderProperty, encoded, options);
153
	}
154
};
155
156
157
// The render API call. Calls the real render function.
158
API.prototype.render = function(){
159
	if(Array.isArray(this._renderProperties)){
160
		for(let i in this._renderProperties){
161
			render(this._renderProperties[i], this._encodings, this._options);
162
		}
163
	}
164
	else{
165
		render(this._renderProperties, this._encodings, this._options);
166
	}
167
168
	return this;
169
};
170
171
API.prototype._defaults = defaults;
172
173
// Prepares the encodings and calls the renderer
174
function render(renderProperties, encodings, options){
175
	encodings = linearizeEncodings(encodings);
176
177
	for(let i = 0; i < encodings.length; i++){
178
		encodings[i].options = merge(options, encodings[i].options);
179
		fixOptions(encodings[i].options);
180
	}
181
182
	fixOptions(options);
183
184
	var Renderer = renderProperties.renderer;
185
	var renderer = new Renderer(renderProperties.element, encodings, options);
186
	renderer.render();
187
188
	if(renderProperties.afterRender){
189
		renderProperties.afterRender();
190
	}
191
}
192
193
// Export to browser
194
if(typeof window !== "undefined"){
195
	window.JsBarcode = JsBarcode;
196
}
197
198
// Export to jQuery
199
/*global jQuery */
200
if (typeof jQuery !== 'undefined') {
201
	jQuery.fn.JsBarcode = function(content, options){
202
		var elementArray = [];
203
		jQuery(this).each(function() {
204
			elementArray.push(this);
205
		});
206
		return JsBarcode(elementArray, content, options);
207
	};
208
}
209
210
// Export to commonJS
211
module.exports = JsBarcode;
212